//
//  QueryViewController.m
//  Express
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "QueryViewController.h"
#import "BaseHeader.h"



@interface QueryViewController ()<SweepCodeViewControllerDelegate, CompanyListTableViewControllerDelegate>

@property (nonatomic, strong) UITextField *orderNumber;
@property (nonatomic, strong) UITextField *company;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) Express *express;



@end

@implementation QueryViewController

- (Express *)express {
    if (!_express) {
        _express = [[Express alloc] init];
    }
    return _express;
}

#pragma mark -------------- deldgate --------------

- (void)returnWithSweepCode:(NSString *)sweepCode {
    self.orderNumber.text = sweepCode;
}

- (void)returnWithExpressCompanyCode:(NSString *)code companyName:(NSString *)name tel:(NSString *)tel {
    self.company.text = name;
    self.code = code;
    self.express.expressTelephone = tel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = [UIColor purpleColor];
    
    _orderNumber = [[UITextField alloc] initWithFrame:CGRectMake(90, kHeight/3, kWidth - 140, 40)];
    _orderNumber.borderStyle = UITextBorderStyleLine;
    _orderNumber.placeholder = @"Please input order number";
    [self.view addSubview:_orderNumber];

    
    UIButton *codeSweep = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [codeSweep setImage:[[UIImage imageNamed:@"barcode_icon@3x"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
    codeSweep.frame = CGRectMake(self.orderNumber.frame.origin.x + self.orderNumber.frame.size.width, self.orderNumber.frame.origin.y, 50, 40);
    [codeSweep addTarget:self action:@selector(sweepCode:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:codeSweep];
    
    
    _company = [[UITextField alloc] initWithFrame:CGRectMake(90, _orderNumber.frame.origin.y + _orderNumber.frame.size.height + 10, kWidth - 140, 40)];
    _company.borderStyle = UITextBorderStyleLine;
    _company.placeholder = @"Plase choose express company";
    [self.view addSubview:_company];
    
    
    UIButton *companyList = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [companyList setImage:[[UIImage imageNamed:@"compangList_icon@3x"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]  forState:(UIControlStateNormal)];
    companyList.frame = CGRectMake(self.company.frame.origin.x + self.company.frame.size.width, self.company.frame.origin.y, 50, 40);
    [companyList addTarget:self action:@selector(TurnToCompanyList:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:companyList];
    
    
    UIButton *queryButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    queryButton.frame = CGRectMake(0, 0, 65, 65);
    queryButton.center = CGPointMake(self.view.center.x, _company.frame.origin.y + _company.frame.size.height + 47);
    [queryButton setBackgroundImage:[[UIImage imageNamed:@"queryButton_icon@3x"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
    [queryButton addTarget:self action:@selector(queryForResult:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:queryButton];
    
    UILabel *expressCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_orderNumber.frame.origin.x - 85, _orderNumber.frame.origin.y, 80, _orderNumber.frame.size.height)];
    expressCodeLabel.text = @"订单号";
    expressCodeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:expressCodeLabel];
    
    UILabel *companyLabel = [[UILabel alloc] initWithFrame:CGRectMake(_company.frame.origin.x - 85, _company.frame.origin.y, 80, _company.frame.size.height)];
    companyLabel.text = @"快递种类";
    companyLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:companyLabel];
    
    
}

- (Express *)setHistoryModel {
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:@[self.code,self.company.text,self.orderNumber.text] forKeys:@[@"expressCode",@"expressName",@"expressOrderNumber"]];
    [self.express setValuesForKeysWithDictionary:dictionary];
//    [[HistoryDataBase shareHistoryDataBase] createDBTableWithName:@"QueryHistory"];
    return _express;
}

#pragma mark -------------- button action --------------

- (void)queryForResult:(UIButton *)button {
    
    CATransition *transition = [CATransition animation];
    transition.type = @"rippleEffect";
    transition.subtype = kCATransitionFade;
    transition.duration = 2;
    [self.view.layer addAnimation:transition forKey:@"waterDrop"];
    QueryResultTableViewController *queryResult = [[QueryResultTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    
    if (self.orderNumber.text != nil && self.code != nil) {
        queryResult.express = [self setHistoryModel];
        [self.navigationController pushViewController:queryResult animated:YES];
    }
    
    
}


- (void)TurnToCompanyList:(UIButton *)button {
    
    CompanyListTableViewController *companyListTVC = [[CompanyListTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    companyListTVC.delegate = self;
    [self.navigationController pushViewController:companyListTVC animated:YES];
}


- (void)sweepCode:(UIButton *)button {
    
    SweepCodeViewController *sweepCodeVC = [[SweepCodeViewController alloc] init];
    sweepCodeVC.delegate = self;
    [self presentViewController:sweepCodeVC animated:YES completion:nil];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
